package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2bPayResult;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryReq;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2bQueryRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;


/** 
 * @ClassName: CmbB2bManager 
 * @Description: 招商银行B2b Manager层业务处理
 * @author w_jiqing 
 * @date 2016年8月29日 下午1:05:35  
 */ 
public class CmbB2bManager implements BankManagerRemoteService{
    
    @Resource(name = "cmbConfig")
    private Map<String, String> cmbConfig;
    
    @Resource(name = "cmbB2bBankServiceClient")
    private BankService cmbB2bBankServiceClient;

    @Override
    public Object sign(BaseMessage msg) throws GopayException {
        CmbB2bMessage message =(CmbB2bMessage) msg;
        message.setMchnbr(message.getMerID());
        message.setReford(message.getOrderId());
        message.setCcynbr(cmbConfig.get("CMB.B2B.PAY.CCYNBR"));
        message.setTrsamt(message.getAmount().toString());
        message.setCrtacc(cmbConfig.get("CMB.B2B.PAY.CRTACC")); 
        message.setCrtnam(cmbConfig.get("CMB.B2B.PAY.CRTNAM"));
        message.setCrtbnk(cmbConfig.get("CMB.B2B.PAY.CRTBNK")); 
        message.setCrtpvc(cmbConfig.get("CMB.B2B.PAY.CRTPVC")); 
        message.setCrtcty(cmbConfig.get("CMB.B2B.PAY.CRTCTY")); 
        message.setOrdcfm(cmbConfig.get("CMB.B2B.PAY.ORDCFM"));
        message.setFinurl(message.getPayRuturnUrl());
        message.setBankUrl(message.getPayUrl());
        message.setPlainMessage(buildSignPlain(message));
        BankLogUtils.printB2BManagerSignSendLog(message);
        message =(CmbB2bMessage) cmbB2bBankServiceClient.sign(message);
        BankLogUtils.printB2BManagerSignReturnLog(message);
        return message;
    }
    
    @Override
    public Object verify(BaseMessage msg) throws GopayException {
        boolean flag = false;
        CmbB2bPayResult messageB2b =(CmbB2bPayResult) msg;
        BankLogUtils.printB2BManagerVerifySendLog(messageB2b);        
        flag=(boolean) cmbB2bBankServiceClient.verify(messageB2b);
        BankLogUtils.printB2BManagerVerifyReturnLog(messageB2b, flag);
        return flag;
    }
    
    @Override
    public Object query(Object obj) throws GopayException {
        CmbB2bQueryRes resMessage= new CmbB2bQueryRes();
        CmbB2bQueryReq message =(CmbB2bQueryReq) obj;
        BankLogUtils.printB2BManagerQuerySendLog(message,message);
        resMessage =(CmbB2bQueryRes) cmbB2bBankServiceClient.query(message);
        resMessage.setOutStlmCd(BankConstants.BANK_LOGO_CMB);
        BankLogUtils.printB2BManagerQueryReturnLog(resMessage);
        return resMessage;
    }

    @Override
    public Object refund(BaseMessage base) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object dayend(BaseMessage base) throws GopayException, Exception {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    /** 
     * @Description: 拼装明文信息
     */
    public String buildSignPlain (CmbB2bMessage message){
        StringBuffer plain =new StringBuffer();
        plain.append("MCHNBR=").append(message.getMchnbr()).append(" ;");
        plain.append("REFORD=").append(message.getReford()).append(" ;");
        plain.append("CCYNBR=").append(message.getCcynbr()).append(" ;");
        plain.append("TRSAMT=").append(message.getTrsamt()).append(" ;");
        plain.append("CRTACC=").append(message.getCrtacc()).append(" ;");
        plain.append("CRTNAM=").append(message.getCrtnam()).append(" ;");
        plain.append("CRTBNK=").append(message.getCrtbnk()).append(" ;");
        plain.append("CRTPVC=").append(message.getCrtpvc()).append(" ;");
        plain.append("CRTCTY=").append(message.getCrtcty()).append(" ;");
        plain.append("ORDCFM=").append(message.getOrdcfm()).append(" ;");
        plain.append("FINURL=").append(message.getFinurl()).append(" ;");
        return plain.toString();
        
    }


}

  
